﻿Imports MySql.Data.MySqlClient
Public Class frmdel_shipper
    Dim mysql As MySqlConnection = main_form.mysqlconection
    Dim mySqlCommand As New MySqlCommand
    Dim mySqlAdaptor As New MySqlDataAdapter
    Dim mySqlReader As MySqlDataReader
    Dim respone As Object
    Dim idvoys As String
    Dim idvoyn As String
    Dim id_primary As String
    Private Sub frmdel_shipper_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US")
        System.Threading.Thread.CurrentThread.CurrentUICulture = System.Threading.Thread.CurrentThread.CurrentCulture
        mysql.Close()

        If mysql.State = ConnectionState.Closed Then
            mysql.Open()
        End If



        mySqlCommand.CommandText = "Select * from shipper;"
        ' mySqlCommand.CommandText = 
        mySqlCommand.Connection = mysql
        mySqlAdaptor.SelectCommand = mySqlCommand

        Try
            mySqlReader = mySqlCommand.ExecuteReader

            While (mySqlReader.Read())

                With ListView1.Items.Add(mySqlReader("SHIPPERID"))

                    .SubItems.Add(mySqlReader("SHIPNAME"))
                    .SubItems.Add(mySqlReader("SHIPADD"))

                    .SubItems.Add(mySqlReader("SHIPTELL"))
                    .SubItems.Add(mySqlReader("SHIPFAX"))

                End With



            End While
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        mysql.Close()


    End Sub

    Public Sub deleteData()
        mysql.Close()
        If mysql.State = ConnectionState.Closed Then
            mysql.Open()
        End If

        If ListView1.SelectedItems.Count > 0 Then
            id_primary = ListView1.SelectedItems(0).SubItems(0).Text
            Dim respone As Object

            respone = MsgBox("ยืนยันข้อมูลถูกต้อง", MsgBoxStyle.OkCancel + MsgBoxStyle.Information, "Warning Messsage")
            If respone = 1 Then


                If mysql.State = ConnectionState.Closed Then
                    mysql.Open()
                End If
                Try

                    mySqlCommand.CommandText = "DELETE FROM shipper where SHIPPERID = '" & id_primary & "';"
                    mySqlCommand.CommandType = CommandType.Text
                    mySqlCommand.Connection = mysql

                    mySqlCommand.ExecuteNonQuery()
                    mysql.Close()
                    searchData()

                Catch ex As Exception

                    MsgBox(ex.ToString)
                    Exit Sub
                End Try

            End If

        End If

    End Sub
    Public Sub searchData()
        mysql.Close()
        If mysql.State = ConnectionState.Closed Then
            mysql.Open()
        End If

        mySqlCommand.CommandText = "Select * from shipper where SHIPNAME like '%" & TextBoxItem2.Text & "%';"
        ' mySqlCommand.CommandText = 
        mySqlCommand.Connection = mysql
        mySqlAdaptor.SelectCommand = mySqlCommand
        Try
            mySqlReader = mySqlCommand.ExecuteReader
            ListView1.Items.Clear()
            While (mySqlReader.Read())

                With ListView1.Items.Add(mySqlReader("SHIPPERID"))

                    .SubItems.Add(mySqlReader("SHIPNAME"))

                    .SubItems.Add(mySqlReader("SHIPTELL"))
                    .SubItems.Add(mySqlReader("SHIPFAX"))

                End With



            End While
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        mysql.Close()
        mysql.Close()
    End Sub
    Private Sub ButtonItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonItem1.Click
        searchData()

    End Sub

    Private Sub TextBoxItem2_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBoxItem2.KeyDown
        If e.KeyCode = Keys.Enter Then
            searchData()
        End If
    End Sub

    Private Sub ButtonX1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonX1.Click
        deleteData()
    End Sub
End Class